package be.donovan.gui.helper.components;

import java.text.NumberFormat;
import java.util.Locale;

import javax.swing.JFormattedTextField;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.NumberFormatter;

public class CurrencyTextField extends JFormattedTextField {
	NumberFormat currencyFormat;
	NumberFormatter numberFormatter;
	
	public CurrencyTextField(){
		super();
		init(Double.MIN_VALUE, Double.MAX_VALUE);
	}
	
	public CurrencyTextField(Double min){
		super();
		init(min, Double.MAX_VALUE);
	}
	
	public CurrencyTextField(Double min, Double max){
		super();
		init(min, max);
	}
	
	private void init(Double min, Double max){
		currencyFormat = NumberFormat.getCurrencyInstance(Locale.FRANCE);		
		numberFormatter = new NumberFormatter(NumberFormat.getNumberInstance());
		numberFormatter.setMinimum(min);
		numberFormatter.setMaximum(max);
		numberFormatter.setValueClass(Double.class);
		setFormatterFactory(new DefaultFormatterFactory(
								new NumberFormatter(currencyFormat),
								new NumberFormatter(currencyFormat),
								numberFormatter));
	}

	public void setInterval(float min, float max){
		currencyFormat = NumberFormat.getCurrencyInstance();
		numberFormatter = new NumberFormatter(NumberFormat.getNumberInstance());
		numberFormatter.setMinimum(new Float(min));
		numberFormatter.setMaximum(new Float(max));

		setFormatterFactory(new DefaultFormatterFactory(
								new NumberFormatter(currencyFormat),
								new NumberFormatter(currencyFormat),
								numberFormatter));
	}
}
